#include<bits/stdc++.h>
using namespace std;

struct node
{
	int x, y, step;
};

int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int vis[301][301];
int mx, qx, qy, zx, zy;

void bfs(int &cnt)
{
	memset(vis, 0, sizeof vis);
	
	queue<node> q;
	q.push({qx, qy, 0});
	vis[qx][qy] = 1;
	
	while(!q.empty())
	{
		node t = q.front();
		q.pop();
		if(t.x == zx && t.y == zy)
		{
			cnt = t.step;
			return ;
		}
		
		for(int i = 0;i < 8;i++)
		{
			int xx = t.x+dx[i];
			int yy = t.y+dy[i];
			if(xx >= 0 && xx < mx && yy >= 0 && yy < mx && !vis[xx][yy])
			{
				q.push({xx, yy, t.step+1});
				vis[xx][yy] = 1;
			}
		}
	}
}

int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		cin>>mx>>qx>>qy>>zx>>zy;
		int cnt = 0;
		bfs(cnt);
		cout<<cnt<<endl;
	}
    return 0;
}

